home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / dvips / RCS / output.c,v < prev    next >
Encoding:
Text File  |  1990-03-02  |  10.9 KB  |  525 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    shirriff:1.2; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     90.03.01.23.24.29;  author shirriff;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     90.02.26.23.24.28;  author shirriff;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @For some reason our printer doesn't like %%PrologEnd lines
  27. @
  28. text
  29. @/*
  30.  *   These routines do most of the communicating with the printer.
  31.  *
  32.  *   LINELENGTH tells the maximum line length to send out.
  33.  */
  34. #define LINELENGTH (78)
  35. #include "structures.h" /* The copyright notice in that file is included too! */
  36. #include <ctype.h>
  37. /*
  38.  *   The external routines called here:
  39.  */
  40. extern void error() ;
  41. extern void send_headers() ;
  42. extern FILE *search() ;
  43. /*
  44.  *   These are the external variables used by these routines.
  45.  */
  46. extern integer hh, vv ;
  47. extern fontdesctype *curfnt ;
  48. extern FILE *bitfile ;
  49. extern char *oname ;
  50. extern Boolean reverse ;
  51. extern Boolean sendcontrolD, disablecomments, multiplesects ;
  52. extern Boolean usesPSfonts, headers_off ;
  53. extern int numcopies ;
  54. extern int totalpages ;
  55. extern integer pagenum ;
  56. extern Boolean manualfeed ;
  57. extern int quiet ;
  58. extern int actualdpi ;
  59. extern char *iname ;
  60. extern char *paperfmt ;
  61. extern char *getenv() ;
  62. extern char errbuf[] ;
  63. extern shalfword linepos ;
  64. /*
  65.  *   We need a few statics to take care of things.
  66.  */
  67. static integer rhh, rvv ;
  68. static Boolean instring ;
  69. static Boolean lastspecial = 1 ;
  70. static shalfword d ;
  71. static int lastfont ;
  72. static void chrcmd();                   /* just a forward declaration */
  73.  
  74. /*
  75.  *   Some very low level primitives to send output to the printer.
  76.  */
  77.  
  78. /*
  79.  *   This routine copies a file down the pipe.  Search path uses the
  80.  *   header path.
  81.  */
  82. static int infigure ;
  83. void
  84. copyfile(s)
  85.         char *s ;
  86. {
  87.    FILE *f ;
  88.    int c, prevc = '\n' ;
  89.  
  90.    if (infigure) {
  91.       f = search(FIGPATH, s) ;
  92.       (void)sprintf(errbuf, "Couldn't find figure file %s; continuing", s) ;
  93.    } else {
  94.       f = search(HEADERPATH, s) ;
  95.       (void)sprintf(errbuf, "! Couldn't find header file %s", s) ;
  96.    }
  97.    if (f==NULL)
  98.       error(errbuf) ;
  99.    else {
  100.       if (! quiet) {
  101.          (void)fprintf(stderr, "[%s]", s) ;
  102.          (void)fflush(stderr) ;
  103.       }
  104.       if (linepos != 0)
  105.          (void)putc('\n', bitfile) ;
  106.       if (! disablecomments)
  107.          (void)fprintf(bitfile, "%%%%BeginDocument: %s\n", s) ;
  108.       while ((c=getc(f))!=EOF) {
  109.          if (c == '%' && prevc == '\n') { /* skip comments */
  110.             while ((c=getc(f))!=EOF) {
  111.                if (c=='\n')
  112.                   break ;
  113.             }
  114.          } else
  115.             (void)putc(c, bitfile) ;
  116.          prevc = c ;
  117.       }
  118.       if (prevc != '\n')
  119.          (void)putc('\n', bitfile) ;
  120.       linepos = 0 ;
  121.       (void)fclose(f) ;
  122.       if (!disablecomments)
  123.          (void)fprintf(bitfile, "%%%%EndDocument\n") ;
  124.    }
  125. }
  126.  
  127. /*
  128.  *   For included PostScript graphics, we use the above routine, but
  129.  *   with no fatal error message.
  130.  */
  131. void figcopyfile(s)
  132. char *s ;
  133. {
  134.    infigure = 1 ;
  135.    copyfile(s) ;
  136.    infigure = 0 ;
  137. }
  138.  
  139. /*
  140.  *   This next routine writes out a `special' character.  In this case,
  141.  *   we simply put it out, since any special character terminates the
  142.  *   preceding token.
  143.  */
  144. void
  145. specialout(c)
  146.         char c ;
  147. {
  148.    if (linepos >= LINELENGTH) {
  149.       (void)putc('\n', bitfile) ;
  150.       linepos = 0 ;
  151.    }
  152.    (void)putc(c, bitfile) ;
  153.    linepos++ ;
  154.    lastspecial = 1 ;
  155. }
  156.  
  157. void
  158. scout(c)   /* string character out */
  159.         char c ;
  160. {
  161.    if (! instring) {
  162. /*
  163.  *   If we are within 5, we send a newline before starting the string.
  164.  *   This eliminates extraneous backslash-newlines.
  165.  */
  166.       if (linepos + 5 > LINELENGTH) {
  167.          (void)putc('\n', bitfile) ;
  168.          linepos = 0 ;
  169.       }
  170.       (void)putc('(', bitfile) ;
  171.       linepos++ ;
  172.       lastspecial = 1 ;
  173.       instring = 1 ;
  174.    }
  175.    if (c<' ' || c>126 || (c=='%' && linepos + 2 > LINELENGTH)) {
  176.       if (linepos + 5 > LINELENGTH) {
  177.          (void)putc('\\', bitfile) ;
  178.          (void)putc('\n', bitfile) ;
  179.          linepos = 0 ;
  180.       }
  181.       (void)putc('\\', bitfile) ;
  182.       linepos++ ;
  183.       (void)putc('0' + ((c >> 6) & 3), bitfile) ;
  184.       linepos++ ;
  185.       (void)putc('0' + ((c >> 3) & 7), bitfile) ;
  186.       linepos++ ;
  187.       (void)putc('0' + (c & 7), bitfile) ;
  188.       linepos++ ;
  189.    } else if (c == '(' || c == ')' || c == '\\') {
  190.       if (linepos + 3 > LINELENGTH) {
  191.          (void)putc('\\', bitfile) ;
  192.          (void)putc('\n', bitfile) ;
  193.          linepos = 0 ;
  194.       }
  195.       (void)putc('\\', bitfile) ;
  196.       linepos++ ;
  197.       (void)putc(c, bitfile) ;
  198.       linepos++ ;
  199.    } else {
  200.       if (linepos + 2 > LINELENGTH) {
  201.          (void)putc('\\', bitfile) ;
  202.          (void)putc('\n', bitfile) ;
  203.          linepos = 0 ;
  204.       }
  205.       (void)putc(c, bitfile) ;
  206.       linepos++ ;
  207.    }
  208. }
  209.  
  210. void
  211. stringend()
  212. {
  213.    instring = 0 ;
  214.    specialout(')') ;
  215. }
  216.  
  217. void
  218. cmdout(s)
  219.         char *s ;
  220. {
  221.    int l ;
  222.  
  223.    /* hack added by dorab */
  224.    if (instring) {
  225.         stringend();
  226.         chrcmd('p');
  227.    }
  228.    l = strlen(s) ;
  229.    if (linepos + l >= LINELENGTH) {
  230.       (void)putc('\n', bitfile) ;
  231.       linepos = 0 ;
  232.       lastspecial = 1 ;
  233.    }
  234.    if (! lastspecial) {
  235.       (void)putc(' ', bitfile) ;
  236.       linepos++ ;
  237.    }
  238.    while (*s != 0) {
  239.       (void)putc(*s++, bitfile) ;
  240.    }
  241.    linepos += l ;
  242.    lastspecial = 0 ;
  243. }
  244.  
  245. static void
  246. chrcmd(c)
  247.         char c ;
  248. {
  249.    if (linepos + 2 > LINELENGTH) {
  250.       (void)putc('\n', bitfile) ;
  251.       linepos = 0 ;
  252.       lastspecial = 1 ;
  253.    }
  254.    if (! lastspecial) {
  255.       (void)putc(' ', bitfile) ;
  256.       linepos++ ;
  257.    }
  258.    (void)putc(c, bitfile) ;
  259.    linepos++ ;
  260.    lastspecial = 0 ;
  261. }
  262.  
  263. void
  264. numout(n)
  265.         integer n ;
  266. {
  267.    char buf[10] ;
  268.  
  269.    (void)sprintf(buf, "%ld", n) ;
  270.    cmdout(buf) ;
  271. }
  272.  
  273. void mhexout(p, len)
  274. register unsigned char *p ;
  275. register long len ;
  276. {
  277.    register char *hexchar = "0123456789ABCDEF" ;
  278.    register int linep ;
  279.    register int n ;
  280.  
  281.    linep = linepos ;
  282.    while (len > 0) {
  283.       if (linep > LINELENGTH - 2) {
  284.          (void)putc('\n', bitfile) ;
  285.          linep = 0 ;
  286.       }
  287.       n = *p++ ;
  288.       (void)putc(hexchar[n >> 4], bitfile) ;
  289.       (void)putc(hexchar[n & 15], bitfile) ;
  290.       linep += 2 ;
  291.       len-- ;
  292.    }
  293.    linepos = linep ;
  294. }
  295.  
  296. void
  297. fontout(n)
  298.         int n ;
  299. {
  300.    char buf[6] ;
  301.  
  302.    if (instring) {
  303.       stringend() ;
  304.       chrcmd('p') ;
  305.    }
  306.    if (n < 27)
  307.       (void)sprintf(buf, "f%c", 'a'+n-1) ;
  308.    else
  309.       (void)sprintf(buf, "f%d", n-27) ;
  310.    cmdout(buf) ;
  311. }
  312.  
  313. void
  314. hvpos()
  315. {
  316.    if (rvv != vv) {
  317.       if (instring) {
  318.          stringend() ;
  319.          numout(hh) ;
  320.          numout(vv) ;
  321.          chrcmd('y') ;
  322.       } else if (rhh != hh) {
  323.          numout(hh) ;
  324.          numout(vv) ;
  325.          chrcmd('a') ;
  326.       } else { /* hard to get this case, but it's there when you need it! */
  327.          numout(vv - rvv) ;
  328.          chrcmd('x') ;
  329.       }
  330.       rvv = vv ;
  331.    } else if (rhh != hh) {
  332.       if (instring) {
  333.          stringend() ;
  334.          if (hh - rhh < 5 && rhh - hh < 5) {
  335.             chrcmd('p' + hh - rhh) ;
  336.          } else if (hh - rhh < d + 5 && rhh - hh < 5 - d) {
  337.             chrcmd('g' + hh - rhh - d) ;
  338.             d = hh - rhh ;
  339.          } else {
  340.             numout(hh - rhh) ;
  341.             chrcmd('b') ;
  342.             d = hh - rhh ;
  343.          }
  344.       } else {
  345.          numout(hh - rhh) ;
  346.          chrcmd('w') ;
  347.       }
  348.    }
  349.    rhh = hh ;
  350. }
  351.  
  352. /*
  353.  *   initprinter opens the bitfile and writes the initialization sequence
  354.  *   to it.
  355.  */
  356. void newline()
  357. {
  358.    if (linepos != 0) {
  359.       (void)fprintf(bitfile, "\n") ;
  360.       linepos = 0 ;
  361.    }
  362. }
  363. void
  364. initprinter()
  365. {
  366.    if (*oname != 0) {
  367. /*
  368.  *   We check to see if the first character is a exclamation
  369.  *   point, and popen if so.
  370.  */
  371.       if (*oname == '!' || *oname == '|') {
  372.          if ((bitfile=popen(oname+1, "w"))==NULL)
  373.             error("! couldn't open output pipe") ;
  374.       } else {
  375.          if ((bitfile=fopen(oname,"w"))==NULL)
  376.             error("! couldn't open PostScript file") ;
  377.       }
  378.    } else {
  379.       bitfile = stdout ;
  380.    }
  381.    if (disablecomments)
  382.       (void)fprintf(bitfile,
  383.              "%%!PS (but not EPSF; comments have been disabled)\n") ;
  384.    else {
  385.       if (multiplesects)
  386.          (void)fprintf(bitfile,
  387.              "%%!PS (but not EPSF because of memory limits)\n") ;
  388.       else  (void)fprintf(bitfile, "%%!PS-Adobe-2.0\n") ;
  389.       (void)fprintf(bitfile, "%%%%Creator: dvips by Radical Eye Software\n") ;
  390.       if (*iname)
  391.          (void)fprintf(bitfile, "%%%%Title: %s\n", iname) ;
  392.       (void)fprintf(bitfile, "%%%%Pages: %d %d\n", totalpages, 1 - 2*reverse) ;
  393.       if (strcmp(paperfmt,"letter")==0)
  394.          (void)fprintf(bitfile, "%%%%BoundingBox: 0 0 612 792\n") ; /*8.5x11*/
  395.       else if (strcmp(paperfmt,"landscape")==0)
  396.          (void)fprintf(bitfile, "%%%%BoundingBox: 0 0 792 612\n") ;
  397.       else if (strcmp(paperfmt,"a4")==0)
  398.          (void)fprintf(bitfile, "%%%%BoundingBox: 0 0 612 842\n") ;
  399.       else (void)fprintf(bitfile, "%%%%BoundingBox: 0 0 612 1008\n"); /*8.5x14*/
  400.       (void)fprintf(bitfile, "%%%%EndComments\n") ;
  401.    }
  402.    if (! headers_off)
  403.       send_headers() ;
  404. }
  405.  
  406. static int endprologsent ;
  407. setup() {
  408.    cmdout("end") ;
  409.    newline() ;
  410.    if (endprologsent == 0 && !disablecomments) {
  411. #if 0
  412.       (void)fprintf(bitfile, "%%%%EndProlog\n") ;
  413. #endif
  414.       (void)fprintf(bitfile, "%%%%BeginSetup\n") ;
  415.       (void)fprintf(bitfile, "%%%%Feature: *Resolution %d\n", DPI) ;
  416.    }
  417.    cmdout("TeXDict") ;
  418.    cmdout("begin") ;
  419.    {  char pft[100] ;
  420.       strcpy(pft, "@@") ;
  421.       strcat(pft, paperfmt) ;
  422.       cmdout(pft) ;
  423.    }
  424.    if (manualfeed) cmdout("@@manualfeed") ;
  425.    if (numcopies != 1) {
  426.       numout((integer)numcopies) ;
  427.       cmdout("@@copies") ;
  428.    }
  429.    if (endprologsent == 0 && !disablecomments) {
  430.       newline() ;
  431.       endprologsent = 1 ;
  432.       (void)fprintf(bitfile, "%%%%EndSetup\n") ;
  433.    }
  434.  
  435. }
  436. /*
  437.  *   cleanprinter is the antithesis of the above routine.
  438.  */
  439. void
  440. cleanprinter()
  441. {
  442.    (void)fprintf(bitfile, "\n") ;
  443.    if (!disablecomments)
  444.       (void)fprintf(bitfile, "%%%%EOF\n") ;
  445.    if (sendcontrolD)
  446.       (void)putc(4, bitfile) ;
  447.    (void)fclose(bitfile) ;
  448.    bitfile = NULL ;
  449. }
  450.  
  451. /*
  452.  *   pageinit initializes the output variables.
  453.  */
  454. static int thispage = 0 ;
  455. void
  456. pageinit()
  457. {
  458.    rhh = rvv = -3141592653 ;
  459.    newline() ;
  460.    if (!disablecomments)
  461.       (void)fprintf(bitfile, "%%%%Page: %ld %d\n", pagenum, ++thispage) ;
  462.    linepos = 0 ;
  463.    cmdout("bop") ;
  464.    lastfont = -1 ;
  465.    d = 0 ;
  466. }
  467.  
  468. /*
  469.  *   This routine ends a page.
  470.  */
  471. void
  472. pageend()
  473. {
  474.    if (instring) {
  475.       stringend() ;
  476.       chrcmd('p') ;
  477.    }
  478.    cmdout("eop") ;
  479. }
  480.  
  481. /*
  482.  *   drawrule draws a rule at the specified position.
  483.  */
  484. void
  485. drawrule(rw, rh)
  486.         shalfword rw, rh ;
  487. {
  488.    hvpos() ;
  489.    if (instring) {
  490.       stringend() ;
  491.       chrcmd('p') ;
  492.    }
  493.    numout((integer)rw) ;
  494.    numout((integer)rh) ;
  495.    chrcmd('v') ;
  496. }
  497.  
  498. /*
  499.  *   drawchar draws a character at the specified position.
  500.  */
  501. void
  502. drawchar(c, cc)
  503.         chardesctype *c ;
  504.         int cc ;
  505. {
  506.    hvpos() ;
  507.    if (lastfont != curfnt->psname) {
  508.       fontout((int)curfnt->psname) ;
  509.       lastfont = curfnt->psname ;
  510.    }
  511.    scout(cc) ;
  512.    rhh = hh + c->pixelwidth ; /* rvv = rv */
  513. }
  514. @
  515.  
  516.  
  517. 1.1
  518. log
  519. @Initial revision
  520. @
  521. text
  522. @d383 1
  523. d385 1
  524. @
  525.